#!/usr/bin/python

import pprint
from sys import argv

# Some globals
diceSides = 20
debug = False

pp = pprint.PrettyPrinter(indent=4)

def getProbDicts(numDice, targetNum, saveDice):

    # Obtain a dict of all possible outcomes
    outcomesDict = getOutcomesDict(numDice)

    # Walk the probability dict to determine the success against
    #   a TN
    walkOutcomes(outcomesDict, targetNum, saveDice)

def walkOutcomes(outcomesDict, targetNum, saveDice):
   
    outcomes = float(len(outcomesDict))
    successDict = {} 
    for x in outcomesDict:
        # x is a list
        successCount = 0
        for y in x:
            if y >= targetNum:
                successCount = successCount+1

        if successCount > saveDice:
            successCount = saveDice

        if successCount in successDict:
            successDict[successCount].append(x)
        else:
            successDict[successCount] = [x]

    for x in successDict.iterkeys():
        succCount = len(successDict[x])
        succPerc = ((succCount) / outcomes ) * 100.0
        print "[%s] success were generated [%s] times, a %s%% chance." % (x, succCount, succPerc)
        if debug:
            for y in successDict[x]:
                print "\t%s" % y

def getOutcomesDict(numDice):
    outcomes = []

    for x in range(1, numDice+1):
        outcomes = appendDiceSides(outcomes, diceSides)

    if debug:
        pp.pprint(outcomes)

    return outcomes

def appendDiceSides(diceList, diceSides):

    if len(diceList) is 0:
        for x in range(1, diceSides+1):
            diceList.append([x])
        return diceList

    newList = []
    for y in diceList:
        # y is an element of list type
        for x in range(1, diceSides+1):
            z = y[:]
            z.append(x)
            newList.append(z)

    return newList

if __name__ == '__main__':
    numDice = 2
    if len(argv) >= 2:
        numDice = int(argv[1])

    targetNum = 4
    if len(argv) >= 3:
        targetNum = int(argv[2])

    saveDice = 2
    if len(argv) >= 4:
        saveDice = int(argv[3])

    probDict = getProbDicts(numDice, targetNum, saveDice)

